package com.zhkj.txg.module.mine.ui

import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.google.gson.Gson
import com.zhkj.lib.base.BalanceChangeEvent
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.RxBus
import com.zhkj.lib.utils.ScreenUtil
import com.zhkj.txg.R
import com.zhkj.txg.base.CustomApplication
import com.zhkj.txg.module.mine.adapter.DiamondMemberAdapter
import com.zhkj.txg.module.mine.entity.ExtendEntity
import com.zhkj.txg.module.mine.entity.UserCardEntity
import com.zhkj.txg.module.mine.vm.DiamondViewModel
import com.zhkj.txg.module.order.entity.OrderPayEntity
import com.zhkj.txg.utils.GridItemDecoration
import com.zhkj.txg.utils.PayUtils
import com.zhkj.txg.utils.WXPayEntity
import kotlinx.android.synthetic.main.activity_balance_opration.*
import kotlinx.android.synthetic.main.activity_diamond_member.*
import kotlinx.android.synthetic.main.activity_diamond_member.viewAliPay
import kotlinx.android.synthetic.main.activity_diamond_member.viewWxPay

class DiamondActivity : ViewModelActivity<DiamondViewModel>() {

    companion object {

        fun open(activity: Activity) {
            val intent = Intent(activity, DiamondActivity::class.java)
            activity.startActivity(intent)
        }
    }

    override fun setViewModel(): DiamondViewModel = createViewModel()

    override fun statusTitleBar(): View? = titleDiamond
    override val layoutResId: Int = R.layout.activity_diamond_member


    var extendEntity: ExtendEntity? = null
    private val diamondMemberAdapter: DiamondMemberAdapter by lazy {
        DiamondMemberAdapter {
            this.extendEntity = it
            btnDiamondPayConfirm.text = "立即支付${it.price}.00"
        }
    }

    override fun initUI() {

        with(tvDiamondMember) {
            layoutManager = GridLayoutManager(context, 3).apply {
                spanSizeLookup.isSpanIndexCacheEnabled = true
//                initialPrefetchItemCount = 6
            }
            addItemDecoration(
                GridItemDecoration(
                    ScreenUtil.dip(this@DiamondActivity, 10),
                    ScreenUtil.dip(this@DiamondActivity, 10)
                )
            )
            adapter = diamondMemberAdapter
        }


        viewWxPay.isSelected = true
        viewAliPay.setOnClickListener {
//            if (intent.getIntExtra(BalanceOperationActivity.TYPE, BalanceOperationActivity.TYPE_RECHARGE) == BalanceOperationActivity.TYPE_WITHDRAW) {
//                viewModel.withdrawConfigLiveData.value?.response?.data?.let {
//                    if (!it.isBindAliPay) return@setOnClickListener
//                }
//            }
            viewAliPay.isSelected = true
            viewWxPay.isSelected = false
        }
        viewWxPay.setOnClickListener {
//            if (intent.getIntExtra(BalanceOperationActivity.TYPE, BalanceOperationActivity.TYPE_RECHARGE) == BalanceOperationActivity.TYPE_WITHDRAW) {
//                viewModel.withdrawConfigLiveData.value?.response?.data?.let {
//                    if (!it.isBindWeChat) return@setOnClickListener
//                }
//            }
            viewAliPay.isSelected = false
            viewWxPay.isSelected = true
        }

        btnDiamondPayConfirm.setOnClickListener {
            userCardEntity?.let { userCard ->
                extendEntity?.let { extend ->
                    viewModel.sumbit(userCard.id.toString(), extend.validDate, CustomApplication.getShopId().toString())
                }
            }
        }

        tvDiamondAgreement.setOnClickListener {

            RechargeActivity.openCZ(this, "会员协议", "https://www.txemart.com/api/article/vip")
        }
    }

    override fun initData() {
        viewModel.getUserCardList()
    }

    var userCardEntity: UserCardEntity? = null

    override fun initViewModel() {
        viewModel.diamondResponse.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) {
                response.response?.data?.forEach {
                    if (it.typeId == "1") {
                        userCardEntity = it
                        tvDiamondExplain.text = it.explain
                        diamondMemberAdapter.setNewData(it.extend)
                    }
                }
            }
        })


        viewModel.submitOrderResponse.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) {
                response.response?.data?.let {
                    viewModel.pay(
                        orderSn = it.orderSn,
                        payType = if (viewAliPay.isSelected) OrderPayEntity.TYPE_ALIPAY else OrderPayEntity.TYPE_WECHAT
                    )
                }
            }
        })

        viewModel.payOrderResponse.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) {
                response.response?.data?.let { order ->
                    when (order.payType) {
                        // 微信支付
                        OrderPayEntity.TYPE_WECHAT -> {
                            order.payInfo.let {
                                PayUtils.wxPay(this, Gson().fromJson(it, WXPayEntity::class.java))
                            }
                        }
                        // 支付宝支付
                        OrderPayEntity.TYPE_ALIPAY -> PayUtils.aliPay(this, order.payInfo, {
                            toast(getString(R.string.pay_success))
                        }, { toast(it) })
                        else -> Unit
                    }
                }
            }
        })
    }
}